package com.wlf.aiinterview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlf.aiinterview.common.ErrorCode;
import com.wlf.aiinterview.constant.UserConstant;
import com.wlf.aiinterview.exception.BusinessException;
import com.wlf.aiinterview.model.pojo.User;
import com.wlf.aiinterview.service.UserService;
import com.wlf.aiinterview.mapper.UserMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-11-11 16:22:23
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Override
    public Integer addUser(String account, String passWord, String checkPassWord) {
        //校验参数
        if (account == null || passWord == null || checkPassWord == null) {
            log.info("存在空参数");
            return UserConstant.FAILED;
        }
        //判断密码是否相同
        if (!passWord.equals(checkPassWord)) {
            log.info("密码不一致");
            return UserConstant.FAILED;
        }
        //加锁保证数据同步
        synchronized (UserService.class) {
            //判断当前账户是否存在
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("userAccount", account);
            User user = this.baseMapper.selectOne(wrapper);
            if (user != null) {
                log.info("当前用户已经存在");
                return UserConstant.FAILED;
            }
            //密码加密插入
            String s = DigestUtils.md5DigestAsHex((passWord + UserConstant.SALT).getBytes());
            User insertUser = new User();
            insertUser.setUseraccount(account);
            insertUser.setUserpassword(s);
            boolean save = this.save(insertUser);
            if (!save) {
                log.info("插入失败");
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "插入失败");
            }
        }
        return UserConstant.SUCCESS;
    }

    @Override
    public Boolean userLogout(HttpServletRequest request) {
        //判断是否登录
        if (request.getSession().getAttribute(UserConstant.SESSIONKEY) == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "未登录");
        }
        request.getSession().removeAttribute(UserConstant.SESSIONKEY);
        return true;
    }

    @Override
    public User getLoginUser(HttpServletRequest request) {
        //判断是否登录
        if (request.getSession().getAttribute(UserConstant.SESSIONKEY) == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "未登录");
        }
        User user = (User) request.getSession().getAttribute(UserConstant.SESSIONKEY);
        return user;
    }


    @Override
    public List<User> userLogin(String account, String password, HttpServletRequest request) {
        //判断参数
        if (account == null || password == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "参数存在空");
        }
        //判断用户是否存在
        String s = DigestUtils.md5DigestAsHex((password + UserConstant.SALT).getBytes());
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("userAccount", account);
        wrapper.eq("userPassword", s);
        User user = this.getOne(wrapper);
        //用户不存在
        if (user == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户不存在");
        }
        //将session改为登录态
        request.getSession().setAttribute(UserConstant.SESSIONKEY, user);
        return List.of(user);
    }

    @Override
    public User getUserById(Long id) {
        //校验参数
        if (id == null || id < 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "id不满足条件");
        }
        //根据id查询
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", id);
        User user = this.getOne(userQueryWrapper);
        if (user == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户不存在");
        }
        return user;
    }

    @Override
    public Page<User> listBypageQuery(int current, int pagesize, String sortField) {
        //校验参数
        if (current < 0 || pagesize < 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "参数不合法");
        }
        //分页查询
        Page<User> page = new Page<>(current, pagesize);
        //查询条件
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (sortField != null&&!sortField.isEmpty()) {
            userQueryWrapper.orderBy(true, true, sortField);
        }
        Page<User> userPage = this.page(page, userQueryWrapper);
        return userPage;
    }
}




